package com.northpool.operator.common.s3;

import cn.hutool.core.util.StrUtil;
import com.northpool.operator.annotation.InputParam;
import com.northpool.operator.annotation.InputParams;
import com.northpool.operator.annotation.OperatorInfo;
import com.northpool.operator.annotation.OutputParam;
import com.northpool.operator.annotation.OutputParams;
import com.northpool.operator.common.algorithm.OperatorTestBuild;
import com.northpool.operator.common.algorithm.TestParamBuilder;
import com.northpool.operator.common.minio.MinIOUtil;
import com.northpool.operator.param.ParamTool;
import com.northpool.operator.type.TypeEnum;
import com.northpool.operator.utils.StpOperatorHelper;

import java.io.ByteArrayInputStream;
import java.nio.charset.StandardCharsets;

@OperatorInfo(ref = "S3FileUploadOperator", name = "文件上传", description = "对象存储文件上传")
@OutputParams({
        @OutputParam(ref = "url", name = "下载地址", desc = "文件下载地址")
})
public class S3FileUploadOperator extends StpOperatorHelper {

    @InputParams({
            @InputParam(ref = "content", name = "文本内容", desc = "上传的文本内容, 如: hello world", required = true),
            @InputParam(ref = "filePath", name = "上传到路径", desc = "上传到路径下面, 如: tmp/result/test.txt", required = true),
            @InputParam(ref = "minioEndpoint", name = "对象存储地址", desc = "http://ip:port", required = true),
            @InputParam(ref = "bucket", name = "桶名称", desc = "桶名称, 如: stp-minio", required = true),
            @InputParam(ref = "accessKey", name = "minio令牌", desc = "minio令牌", type = TypeEnum.STRING, required = true),
            @InputParam(ref = "secretKey", name = "minio密钥", desc = "minio密钥", type = TypeEnum.STRING, required = true),
            @InputParam(ref = "urlPrefix", name = "下载地址路径前缀", desc = "下载地址路径前缀,适用于对象存储地址对外不可访问的情况, 如: http://192.168.1.1:8021/stp-minio", type = TypeEnum.STRING, required = true),
    })
    public S3FileUploadOperator() {
    }

    public static void main(String[] args) throws Exception {
        OperatorTestBuild.execute(S3FileUploadOperator.class, TestParamBuilder.aBuilder()
                .param("content", "hello world")
                .param("filePath", "tmp/result/test.txt")
                .param("minioEndpoint", "http://northpool.work:9000")
                .param("bucket", "stp-minio")
                .param("accessKey", "northpool")
                .param("secretKey", "piesat@123")
                .param("urlPrefix", "http://northpool.work:9000")
                .build());
    }

    @Override
    public void execute(ParamTool paramTool) throws Exception {
        String content = paramTool.getStr("content");
        String path = paramTool.getStr("filePath").trim();
        String minioEndpoint = paramTool.getStr("minioEndpoint");
        String bucket = paramTool.getStr("bucket");
        String accessKey = paramTool.getStr("accessKey");
        String secretKey = paramTool.getStr("secretKey");
        String urlPrefix = paramTool.getStr("urlPrefix");

        MinIOUtil minIOUtil = new MinIOUtil(minioEndpoint, accessKey, secretKey);
        if (!minIOUtil.bucketExists(bucket)) {
            throw new RuntimeException("桶不存在：" + bucket);
        }

        ByteArrayInputStream inputStream = new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8));
        minIOUtil.putObject(bucket, path, inputStream);
        log("上传完成: {}", path);

        String url = StrUtil.format("{}/{}/{}", urlPrefix, bucket, path);
        this.result("url", url);
    }
}
